﻿package Welsy4.Connect.commands {
	import Welsy4.Connect.business.PermissionDelegate;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.model.ConnectModel;

	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;

	import flash.net.URLVariables;

	import mx.rpc.IResponder;



	public class AddMeetingParticipantsCommand extends SequenceCommand implements Command, IResponder {

		public function AddMeetingParticipantsCommand() {

		}

		override public function execute(event : CairngormEvent) : void {
			ConnectModel.getInstance().addQueue("AddMeetingParticipantsCommand.execute ");

			this.nextEvent = ChainEvent(event).nextChainedEvent;

			var publicPermissionRequest : URLVariables = new URLVariables();
			var permission : String = ConnectModel.getInstance().lastUserFound.permission;

			if (ChainEvent(event).eventRequest.permission != null)
				permission = ChainEvent(event).eventRequest.permission;
			// set request GET params
			publicPermissionRequest.action = "permissions-update";
			publicPermissionRequest.decode("acl-id=" + ConnectModel.getInstance().lastMeetingFound.sco_id);
			publicPermissionRequest.decode("principal-id=" + ConnectModel.getInstance().lastUserFound.principal_id);
			publicPermissionRequest.decode("permission-id=" + permission);

			// set Delegate to send data
			var permissionDelegate : PermissionDelegate = new PermissionDelegate(this);
			permissionDelegate.setPermission(publicPermissionRequest);

		}

		public function result(event : Object) : void {

			// listen for ok result
			ConnectModel.getInstance().addQueue("AddMeetingParticipantsCommand.result " + event.result);

			this.executeNextCommand();
		}

		public function fault(event : Object) : void {
			ConnectModel.getInstance().addQueue("AddMeetingParticipantsCommand.fault");
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.CONNECTION));
		}
	}

}